//
//  FBSubmitHandle.m
//  UILbrary
//
//  Created by Apple on 2023/3/4.
//  Copyright © 2023 mac. All rights reserved.
//

#import "FBSubmitHandle.h"

@interface FBSubmitHandleView : UIView

@property(nonatomic, strong) UIStackView *stackView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, copy) void(^submitBlock)(void);

@end


@implementation FBSubmitHandleView

//MARK: - system

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_configSubviews];
//        [self test];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    [self p_draw];
    
}

- (void)p_draw
{
    UIColor *startColor = [UIColor colorWithHexString:@"#ed776e"];
    UIColor *endColor = [UIColor colorWithHexString:@"#ea3627"];

    self.button.backgroundColor = [UIColor gradientColorWithSize:self.button.frame.size
                                                       direction:GradientColorDirectionLevel
                                                      startColor:startColor
                                                        endColor:endColor];
    self.button.layer.cornerRadius = self.button.bounds.size.height / 2.0;
}

//MARK: - test

- (void)test
{
    self.stackView.backgroundColor = [UIColor systemCyanColor];
}

//MARK: - private

- (void)p_configSubviews
{
    [self addSubview:self.stackView];
    [self.stackView addArrangedSubview:self.button];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.stackView);
    }];
    
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, Space_big, 0, Space_big));
    }];
}

//MARK: - action

- (void)submitButtonAction
{
    if(!self.submitBlock) return;
    self.submitBlock();
}

//MARK: - get

- (UIStackView *)stackView
{
    if(!_stackView){
        _stackView = [UIStackView new];
        _stackView.axis = UILayoutConstraintAxisHorizontal;
        _stackView.distribution = UIStackViewDistributionFill;
        _stackView.alignment = UIStackViewAlignmentCenter;
        _stackView.spacing = 10;
    }
    return _stackView;
}

- (UIButton *)button
{
    if(!_button){
        _button = [UIButton new];
        _button.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_button setTitle:@"提交" forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(submitButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
    return _button;
}
@end

@interface FBSubmitHandle()

@property(nonatomic, strong) FBSubmitHandleView *p_view;

@end


@implementation FBSubmitHandle

//MARK: - FBCompleteProtocol


- (void)completeHandle:(NSObject *)obj
{
    NSLog(@"发起网络请求 参数: %@",obj);
}


//MARK: - get

- (FBSubmitHandleView *)p_view
{
    if(!_p_view){
        _p_view = [FBSubmitHandleView new];
        @WeakObj(self)
        _p_view.submitBlock = ^{
            @StrongObj(self)
            [self.checkHandle completeHandle:nil];
        };
    }
    return _p_view;
}

- (UIView *)view
{
    return self.p_view;
}
@end
